package com.morningstar.flow.antvX6.edge;

import com.alibaba.fastjson2.JSON;
import com.morningstar.flow.antvX6.common.Node;
import com.morningstar.flow.antvX6.common.NodeParser;
import lombok.Getter;
import lombok.Setter;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.SequenceFlow;

import java.util.HashMap;

@Getter
@Setter
public class Edge extends Node implements NodeParser {

    public static final String TYPE = "edge";

    private EdgePoint source;

    private EdgePoint target;

    @Override
    public String getNodeType() {
        return TYPE;
    }

    @Override
    public String getNodeName() {
        return "连线";
    }

    @Override
    public void convertAndAdd(Process process) {
        SequenceFlow sequenceFlow = new SequenceFlow();
        sequenceFlow.setId(this.getCName());
        sequenceFlow.setSourceRef(this.getSource().getCName());
        sequenceFlow.setTargetRef(this.getTarget().getCName());
        if (this.getData() != null && this.getData().getConditionGroup() != null) {
            sequenceFlow.setConditionExpression("${conditionGroupHandler.handle(execution)}");
        } else {
            sequenceFlow.setConditionExpression("${12==12}");
        }
        // TODO 没有条件 类型判断
        process.addFlowElement(sequenceFlow);
    }

    @Override
    public Node parse(HashMap<String, Object> cell) {
        return JSON.parseObject(JSON.toJSONString(cell), Edge.class);
    }
}
